package com.kkd.kkdapp.adapter;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.DealDetailsActivity;
import com.kkd.kkdapp.base.MyBaseAdapter;
import com.kkd.kkdapp.utils.LogUtil;

public class GridViewAdapater extends MyBaseAdapter<String> {

	private DealDetailsActivity dealDetailsActivity;
	private TextView classify_tv;
	private int mPosition = 0;

	public GridViewAdapater(DealDetailsActivity dealDetailsActivity,
			TextView classify_tv) {
		super(dealDetailsActivity);
		this.dealDetailsActivity = dealDetailsActivity;
		this.classify_tv = classify_tv;
	}

	@Override
	public View getMyView(int position, View convertView, ViewGroup parent) {
		ViewHodler viewhodler;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.gridview_item, null);
			viewhodler = new ViewHodler();
			viewhodler.bg_tv = (TextView) convertView.findViewById(R.id.bg_tv);
			viewhodler.item_tv = (TextView) convertView
					.findViewById(R.id.item_tv);
			convertView.setTag(viewhodler);
		} else {
			viewhodler = (ViewHodler) convertView.getTag();
		}

		viewhodler.bg_tv.setBackgroundResource(R.color.violet_ddd8ec);
		if (mPosition != position) {
			viewhodler.bg_tv.setBackgroundResource(R.color.white);
		}
		String option = this.getAdapterData().get(position);
		viewhodler.item_tv.setText(option);
		convertView.setOnClickListener(new GridViewOnClickListener(position,
				viewhodler.bg_tv));

		return convertView;
	}

	static class ViewHodler {
		TextView item_tv;
		TextView bg_tv;
	}

	class GridViewOnClickListener implements OnClickListener {

		int position;
		TextView tv;

		public GridViewOnClickListener(int position, TextView tv) {
			this.position = position;
			this.tv = tv;
		}

		@Override
		public void onClick(View v) {
			if (mOnGridViewItemCliclistener != null) {
				mOnGridViewItemCliclistener.onPosition(position);
				tv.setBackgroundResource(R.color.violet_ddd8ec);
				mPosition = position;
			}
		}
	}

	OnGridViewItemCliclistener mOnGridViewItemCliclistener;

	public void setonGridViewItemCliclistener(
			OnGridViewItemCliclistener callback) {
		this.mOnGridViewItemCliclistener = callback;
	}

	public interface OnGridViewItemCliclistener {
		public void onPosition(int position);
	}
}
